package com.tencent.backstage.dao.mongoDao;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.tencent.backstage.enums.SourceEnum;
import com.tencent.backstage.vo.DBObjectList;
import com.tencent.backstage.vo.PNMongoVO;
import com.tencent.backstage.vo.RankingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;
import java.util.*;
@Repository
public class UserRecordDao {
    @Autowired
    private MongoTemplate mongoTemplate;
    public DBObjectList getFirstRecordLimit(Integer limit,Integer skip) {
        DBCollection dbCollection = mongoTemplate.getCollection("personResultMongo");
        DBObject sortTime = new BasicDBObject("createTime",-1);
        long size =  dbCollection.getCount();
        DBCursor dbObjects =  dbCollection.find().sort(sortTime).skip(skip).limit(limit);
        DBObjectList dbObjectList = new DBObjectList();
        dbObjectList.setList(dbObjects.toArray());
        dbObjectList.setTotalSize(Integer.valueOf(String.valueOf(size)));
        return dbObjectList;
    }
    public List<RankingVO> getTypeRanking() {
        DBCollection dbCollection = mongoTemplate.getCollection("personResultMongo");
        List<RankingVO> list = new ArrayList<>();
        for (SourceEnum sourceEnum : SourceEnum.values()) {
            DBObject type = new BasicDBObject("type", sourceEnum.getMessage());
            int size =   dbCollection.find(type).size();
            RankingVO rankingVO = new RankingVO();
            rankingVO.setTypeName(sourceEnum.getMessage());
            rankingVO.setNumber(size);
            list.add(rankingVO);
        }
        // 按使用次数数倒序
        Collections.sort(list, new Comparator<RankingVO>() {
            public int compare(RankingVO arg0, RankingVO arg1) {
                int hits0 = arg0.getNumber();
                int hits1 = arg1.getNumber();
                if (hits1 > hits0) {
                    return 1;
                } else if (hits1 == hits0) {
                    return 0;
                } else {
                    return -1;
                }
            }
        });
        return list;
    }
    public PNMongoVO getPN() {
        DBCollection dbCollection = mongoTemplate.getCollection("personResultMongo");
        DBObject right = new BasicDBObject("result.ret",0);
        DBCursor dbObjects =  dbCollection.find(right);
        PNMongoVO pnMongoVO = new PNMongoVO();
        pnMongoVO.setRight(dbObjects.size());
        pnMongoVO.setAllNumber(Integer.valueOf(String.valueOf(dbCollection.getCount())));
        pnMongoVO.setError(Integer.valueOf(String.valueOf(dbCollection.getCount()))-dbObjects.size());
        return pnMongoVO;
    }
    public DBObjectList findById(String id) {
        DBCollection dbCollection = mongoTemplate.getCollection("personResultMongo");
        DBObject right = new BasicDBObject("_id",id);
        DBCursor dbObjects =  dbCollection.find(right);
        DBObjectList dbObjectList = new DBObjectList();
        dbObjectList.setList(dbObjects.toArray());
        dbObjectList.setTotalSize(dbObjects.size());
        return dbObjectList;
    }
    public DBObjectList reloadPicture(Integer limit, Integer skip) {
        DBCollection dbCollection = mongoTemplate.getCollection("personResultMongo");
        DBObject sortTime = new BasicDBObject("createTime",-1);
        DBCursor dbCursor =  dbCollection.find().sort(sortTime).limit(limit).skip(skip);
        long size =  dbCollection.getCount();
        DBObjectList dbObjectList = new DBObjectList();
        dbObjectList.setList(dbCursor.toArray());
        dbObjectList.setTotalSize(Integer.valueOf(String.valueOf(size)));
        return dbObjectList;
    }
    public DBObjectList findByOpenid(String openid) {
        DBCollection dbCollection = mongoTemplate.getCollection("personResultMongo");
        DBObject sortTime = new BasicDBObject("createTime",-1);
        DBObject openId = new BasicDBObject("openid",openid);
        DBCursor dbCursor =  dbCollection.find(openId).sort(sortTime);
        long size =  dbCollection.getCount();
        DBObjectList dbObjectList = new DBObjectList();
        dbObjectList.setList(dbCursor.toArray());
        dbObjectList.setTotalSize(Integer.valueOf(String.valueOf(size)));
        return dbObjectList;
    }
}
